package it.blueocean.acanto.platform.web;

import it.blueocean.acanto.entities.platform.DBUser;
import it.blueocean.acanto.platform.service.NavigationService;
import it.blueocean.acanto.taglibrary.jsp.taglib.navigation.MenuList;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class NavigationController
{
	private final static Logger logger = LoggerFactory.getLogger(NavigationController.class);

	@Inject
	@Named("navigationService")
	NavigationService navigationService;

	@RequestMapping(method = RequestMethod.GET, value = "/navigation.htm")
	public String navigateTo(@RequestParam("__menu_id") String menuId, HttpSession session)
	{
		logger.info("invoked navigation.htm");
		
		MenuList localMenuList = (MenuList)session.getAttribute("NAVIGATION_TREE");
		
		if (localMenuList == null)
	    {
			DBUser user = (DBUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			localMenuList = navigationService.getFilteredMenu(user.getAuthorities());
			session.setAttribute("NAVIGATION_TREE", localMenuList);
	    }

		localMenuList.setSelected(menuId);

		return "forward:" + localMenuList.getUrl(menuId);
	}
}
